package com.msite

/**
 * Created by tpeng on 2015/10/15.
 */
class Encrypt {

    public static void main(String[] args) {

        //EncryptFile("f:\\Mr.Peng\\890314_0.xlsx", "890314.xlsx.bin");
       // DecryptFile("H:\\Mr.Peng\\890314.xlsx.bin", "890314_1.xlsx");

    }

    public static String toStringHex(String s)
    {
        byte[] baKeyword = new byte[s.length()/2];
        for(int i = 0; i < baKeyword.length; i++)
        {
            try
            {
                baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16));
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        try
        {
            s = new String(baKeyword, "utf-8");//UTF-16le:Not
        }
        catch (Exception e1)
        {
            e1.printStackTrace();
        }
        return s;
    }

    static void DecryptFile(def path, def fname) {
        def f = new File(path)
        def file = new RandomAccessFile(f, "r");
        def out = new FileOutputStream(new File(f.parentFile.path +"\\"+fname))

        def arry = [5,6,3,1,4,2,0] as int[];
        def vidx = (f.length() / 7).longValue();
        def b = new byte[vidx];
        arry.each {
            k ->
                file.seek(vidx * k)
                file.read(b)
                out.write(b)
        }
        if(f.length() > vidx * 6){
            file.seek(vidx * 7)
            file.read(b)
            out.write(b, 0, (f.length() - vidx * 7).intValue())
        }
        out.flush();
        out.close();
    }

    static void EncryptFile(def path, def fname) {
        def f = new File(path)
        def file = new RandomAccessFile(f, "r");
        def out = new FileOutputStream(new File(f.parentFile.path +"\\"+fname))

        def arry = [6,3,5,2,4,0,1] as int[];
        def vidx = (f.length() / 7).longValue();
        def b = new byte[vidx];
        arry.each {
            k ->
                file.seek(vidx * k)
                file.read(b)
                out.write(b)
        }
        if(f.length() > vidx * 6){
            file.seek(vidx * 7)
            file.read(b)
            out.write(b, 0, (f.length() - vidx * 7).intValue())
        }
        out.flush();
        out.close();
    }

}
